package com.group.itms.routes.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.group.itms.routes.utils.Convert;
import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.field.ForeignCollectionField;
import com.j256.ormlite.table.DatabaseTable;

@DatabaseTable
public class Topic {

	public static final String IS_ACTIVE = "isActive";
	
	@DatabaseField(id = true)
	private int id;
	@DatabaseField
	private String name;
	@DatabaseField(columnName = IS_ACTIVE)
	private boolean isActive;
	@DatabaseField
	private Date insertTime;

	@ForeignCollectionField
	private ForeignCollection<BObjectTopic> objectTopics;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	public Date getInsertTime() {
		return insertTime;
	}

	public void setInsertTime(Date insertTime) {
		this.insertTime = insertTime;
	}

	public List<BObject> getObjects() {
		List<BObject> objects = new ArrayList<BObject>();
		List<BObjectTopic> objTopics = Convert.toList(objectTopics);
		for (BObjectTopic ot : objTopics) {
			if (!ot.isActive()) {
				continue;
			}
			BObject obj = ot.getObject();
			if (obj != null) {
				objects.add(obj);
			}
		}
		return objects;
	}
	
	@Override
	public String toString() {
		int size = objectTopics != null ? objectTopics.size() : 0;
		return name + " (" + size + ")";
	}
	
	@Override
	public boolean equals(Object o) {
		if (!(o instanceof Topic)) {
			return false;
		}
		Topic other = (Topic) o;
		return id == other.id;
	}
}
